package restful.struts2;

import java.io.*;

import javax.servlet.*;

public class Utf8Filter implements Filter {

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		charsetEncoding(request, response, "utf-8");
		proceed(request, response, chain);
	}

	private void charsetEncoding(ServletRequest request, ServletResponse response, String encoding) throws UnsupportedEncodingException {
		charsetEncoding(request, encoding);
		charsetEncoding(response, encoding);
	}

	private void proceed(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		chain.doFilter(request, response);
	}

	private void charsetEncoding(ServletResponse response, String encoding) {
		response.setCharacterEncoding(encoding);
	}

	private void charsetEncoding(ServletRequest request, String encoding) throws UnsupportedEncodingException {
		request.setCharacterEncoding(encoding);
	}

	public void init(FilterConfig filterConfig) throws ServletException {}

	public void destroy() {}
}
